<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 
    编写一个方法 queryUrlParmeter,实现把一个URL地址问号传参部分的信息获取到,并且解析为对象键值对的方式,例如：
    URL地址：
      'https://www.baidu.com/s?wd=javascript&rsv_spt=1'
    解析结果为:
      {wd:'javascript',rsv_spt:1}
    要掌握的字符串的常用方法： charAt charCodeAt substr substring slice ...
   -->
</head>

<body>
    <script>
        let url = 'https://www.baidu.com/s?wd=javascript&rsv_spt=1';

        function queryUrlParmeter(url) {
            let obj = {};
            if (url.indexOf('?') === -1) return obj;
            // url = url.split('?')[1];
            // url = url.split('&');
            url = url.split('?')[1].split('&');
            for (let i = 0; i < url.length; i++) {
                let item = url[i].split('=');
                obj[item[0]] = item[1];
            }
            return obj;
        }
        let result = queryUrlParmeter(url);
        console.log(result);
        // let url = 'https://www.baidu.com/s?wd=javascript&rsv_spt=1';
        // String.prototype.getUrl = function() {
        //     let obj = {};
        //     let item = this.split('?')[1].split('&');
        //     for (var i = 0; i < item.length; i++) {
        //         obj[item[i].split('=')[0]] = item[i].split('=')[1];
        //     }
        //     return obj;
        // }
        // let res = url.getUrl();
        // console.log(res);
        // res = null;
    </script>
</body>

</html>